/**
 * 
 */
package ch.unizh.ifi.ailab.dataTypes;

/**
 * vector class to make 2 dimensional vectors useable.
 * 
 * coordinates: x = horizontal, y = vertical
 * 
 * @author hans jaeckle - jaeckle@gmail.com
 * 
 */
public class Vector2D {
	private double xVal;

	private double yVal;

	public Vector2D() {
		xVal = 0.0;
		yVal = 0.0;
	}

	public Vector2D(Vector2D vector) {
		setXVal(vector.getXVal());
		setYVal(vector.getYVal());
	}

	public Vector2D(double xVal, double yVal) {
		setXVal(xVal);
		setYVal(yVal);
	}

	public Vector2D(int norm, double angle) {
		setXVal(norm * Math.sin(angle));
		setYVal(norm * Math.cos(angle));
	}

	/**
	 * adds the parameter vector to this vector
	 * 
	 * @param vector
	 */
	public void plus(Vector2D vector) {
		setXVal(getXVal() + vector.getXVal());
		setYVal(getYVal() + vector.getYVal());
	}

	/**
	 * multiplies this vector with the parameter number
	 * 
	 * @param number
	 */
	public void mult(double number) {
		setXVal(getXVal() * number);
		setYVal(getYVal() * number);
	}

	/**
	 * devides this vector trough the parameter number
	 * 
	 * @param number
	 */
	public void div(double number) {
		setXVal(getXVal() / number);
		setYVal(getYVal() / number);
	}

	/**
	 * subtracts the parameter vector from this vector
	 * 
	 * @param vector
	 */
	public void minus(Vector2D vector) {
		setXVal(getXVal() - vector.getXVal());
		setYVal(getYVal() - vector.getYVal());
	}

	/**
	 * returns the norm of this vector
	 * 
	 * @return norm
	 */
	public double getNorm() {
		double norm = Math.sqrt((getXVal() * getXVal())
				+ (getYVal() * getYVal()));
		return norm;
	}

	/**
	 * returns the polar angle of this vector
	 * 
	 * @return angle in in rad
	 */
	public double getAngle() {
		double norm = getNorm();
		if (norm != 0.0) {
			return Math.asin(getXVal() / getNorm());
		}
		return 0.0;
	}

	public double getXVal() {
		return xVal;
	}

	public double getYVal() {
		return yVal;
	}

	public void setXVal(double xVal) {
		this.xVal = xVal;
	}

	public void setYVal(double yVal) {
		this.yVal = yVal;
	}

	/**
	 * checks if this is a null vector
	 * 
	 * @return true if this is a null vector, false else
	 */
	public boolean isNull() {
		return (getXVal() != 0.0 || getYVal() != 0.0);
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("x: " + getXVal());
		sb.append(" y: " + getYVal());
		sb.append(" angle: " + Math.toDegrees(getAngle()));
		sb.append(" norm: " + getNorm());
		return sb.toString();
	}
}
